import { getUserInfoDetailAPI } from '@/api/user'

const userInfo = {
  state: {
    openid: '',
    userProfile: {
      fansId: '',
      hasUnionid: true,
      username: '',
      avator: '',
      cashBalance: 0
    }
  },

  mutations: {
    SET_OPENID: (state, openid) => {
      state.openid = openid
    },
    SET_USERPROFILE: (state, userProfile) => {
      state.userProfile.fansId = userProfile.fansId
      state.userProfile.hasUnionid = userProfile.hasUnionid,
      state.userProfile.username = userProfile.username
      state.userProfile.avator = userProfile.avator
      state.userProfile.cashBalance = userProfile.cashBalance
    },
  },

  actions: {
    getUserProfile({ commit }) {
      return new Promise((resolve, reject) => {
        getUserInfoDetailAPI().then(({ data }) => {
          const userProfile = {
            fansId: data.id,
            hasUnionid: data.has_unionid,
            username: data.username,
            avator: data.headimgurl,
            cashBalance: data.cash_balance
          }

          commit('SET_USERPROFILE', userProfile)

          resolve({ userProfile })
        }).catch(error => {
          reject(error)
        })
      })
    }
  }
}

export default userInfo
